
CROSS_COMPILE 		?= arm-linux-gnu- 
TARGET 				?= irq


OBJS 				:= start.o main.o
GCC 				:= $(CROSS_COMPILE)gcc
LD 					:= $(CROSS_COMPILE)ld
OBJCOPY 			:= $(CROSS_COMPILE)objcopy
OBJDUMP 			:= $(CROSS_COMPILE)objdump

INCLUDEDIRS 		:= imx6u 		\
						bsp/clk 	\
						bsp/led 	\
						bsp/delay

SRCDIRS 			:= project 		\
						bsp/clk 	\
						bsp/led 	\
						bsp/delay


INCLUDE 			:= $(patsubst %, -I %, $(INCLUDEDIRS))

SFILES 				:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.s))
CFILES 				:= $(foreach dir, $(SRCDIRS), $(wildcard $(dir)/*.c))

SFILENDIR 			:= $(notdir $(SFILES))
CFILENDIR 			:= $(notdir $(CFILES))

SOBJS 				:= $(patsubst %,  obj/%, $(SFILENDIR:.s=.o))
COBJS 				:= $(patsubst %,  obj/%, $(CFILENDIR:.c=.o))

OBJS 				:= $(SOBJS)$(COBJS)

VPATH 				:= $(SRCDIRS)

.PHONY:clean

$(TARGET).bin : $(OBJS)
	$(LD) -Timx6u.lds -o $(TARGET).elf $^
	$(OBJCOPY) -O binary -S $(TARGET).elf $@
	$(OBJDUMP) -D -m arm $(TARGET).elf > $(TARGET).dis

$(SOBJS) : obj/%.o : %.s
	$(GCC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<

$(COBJS) : obj/%.o : %.c
	$(GCC) -Wall -nostdlib -c -O2 $(INCLUDE) -o $@ $<


print:
	@echo INCLUDE = $(INCLUDE)
	@echo SFILES = $(SFILES)
	@echo CFILES = $(CFILES)
	@echo SFILENDIR = $(SFILENDIR)
	@echo CFILENDIR = $(CFILENDIR)
	@echo SOBJS = $(SOBJS)
	@echo COBJS = $(COBJS)
	@echo OBJS = $(OBJS)

clean :
	rm -rf *.o $(TARGET).bin $(TARGET).elf $(TARGET).dis

